package team.kirohuji.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import team.kirohuji.domain.Attendance;
import team.kirohuji.domain.AttendanceRecord;
import team.kirohuji.domain.SystemRole;
import team.kirohuji.domain.SystemUser;
import team.kirohuji.domain.UserInfo;
import team.kirohuji.service.AttendanceRecordService;
import team.kirohuji.service.AttendanceService;
import team.kirohuji.service.SystemUserRoleService;
import team.kirohuji.service.SystemUserService;
import team.kirohuji.service.UserInfoService;
import team.kirohuji.utils.PageUtils;
import team.kirohuji.utils.R;

/**
 * 这个Controller用户管理登录
 * 
 * @author kirohuji
 * @version 0.1
 */
@Controller
public class UserManagerController {
	 private static final Logger logger = LoggerFactory.getLogger(UserManagerController.class);
	/**
	 * @see SystemUserService
	 */
	@Resource
	private SystemUserService systemUserService;

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private AttendanceService attendanceService;
	
	@Resource
	private AttendanceRecordService attendanceRecordService;
	/**
	 * @see SystemUserRoleService
	 */
	@Resource
	private SystemUserRoleService systemUserRoleService;

	// 添加新管理员
	/**
	 * 添加新管理员
	 * 
	 * @param request
	 *            HttpServlet请求
	 * @return Map对象
	 */
	@ResponseBody
	@RequestMapping(value = "/addUser", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	public Map<String, Object> addUser(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String userName = request.getParameter("userName");
		SystemUser su = new SystemUser();
		su.setLoginName(loginName);
		su.setPassword(password);
		su.setSex(sex);
		su.setUserName(userName);
		List<SystemRole> systemRoles = new ArrayList<>();
		String[] systemRoleIdStringList = request.getParameter("systemRoles").split("-");
		List<Integer> systemRoleId = new ArrayList<>(systemRoleIdStringList.length);
		for (int i = 0; i < systemRoleIdStringList.length; i++) {
			systemRoleId.add(Integer.parseInt(systemRoleIdStringList[i]));
		}
		su.setRoleIdList(systemRoleId);
		System.out.println(su.getRoleIdList());
		su.setSystemRoles(systemRoles);
		systemUserService.save(su);
		Map<String, Object> map = new HashMap<>();
		if (su != null) {
			map.put("message", "success");
			map.put("user", su);
		} else {
			map.put("message", "登录名已经存在");
		}
		return map;
	}

	/**
	 * 修改管理员角色
	 * 
	 * @param request
	 *            HttpServlet请求
	 * @return Map对象
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyRole", method = RequestMethod.GET)
	public R modifyRole(@RequestParam(value = "userid") Integer userId,
			@RequestParam(value = "roleIdList") String roleIdList) {
		String[] systemRoleIdStringList = roleIdList.split("-");
		List<Integer> systemRoleId = new ArrayList<>(systemRoleIdStringList.length);
		for (int i = 0; i < systemRoleIdStringList.length; i++) {
			systemRoleId.add(Integer.parseInt(systemRoleIdStringList[i]));
		}
		systemUserRoleService.saveOrUpdate(userId, systemRoleId);
		return R.ok().put("message", "success");
	}

	/**
	 * 分页获取数据
	 * 
	 * @param page
	 *            第几页
	 * @param limit
	 *            每页最长记录数
	 * @return R {@link R}
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public R list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		// 查询列表数据
		List<SystemUser> List = systemUserService.queryList(map);
		int total = systemUserService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(List, total, limit, page);
		return R.ok().put("page", pageUtil);

	}
	@RequestMapping(value = "/userInfo", method = RequestMethod.GET)
	@ResponseBody
	public R userInfo(@RequestParam(value = "userInfoId") Integer  userInfoId) {
		Map<String,Object> map=new HashMap<>();
		map.put("userInfoId", userInfoId);
		List<UserInfo> list=userInfoService.queryList(map);
		return R.ok().put("list", list);
	}
	@RequestMapping(value = "/attendance", method = RequestMethod.GET)
	@ResponseBody
	public R attendance(@RequestParam(value = "systemUserId") Integer  systemUserId) {
		Attendance attendance=attendanceService.queryObjectByUserId(systemUserId);
		if(attendance!=null) {
			attendance.setCheckStatus("打卡");
			attendance.setStatus("考勤成功");
		}else {
			attendance=new Attendance();
			attendance.setCheckStatus("打卡");
			attendance.setStatus("考勤成功");
		}
		attendanceService.saveOrUpdate(attendance);
		
		AttendanceRecord attendanceRecord=new AttendanceRecord();
		attendanceRecord.setCheckDate(new Date());
		attendanceRecord.setComment("打卡");
		attendanceRecord.setLocation("定位");
		attendanceRecord.setStatus(1);
		attendanceRecord.setType(1);
		attendanceRecordService.saveOrUpdate(attendanceRecord);
		SystemUser systemUser=systemUserService.queryObject(systemUserId);
		logger.info(systemUser.getLoginName()+":"+systemUser.getId()+"成功打卡");
		return R.ok();
	}
	
}
